package com.ugoodtech.mdcc.web.controller;

        /* Copyright (C) Ugoodtech, Inc - All Rights Reserved.
         * Unauthorized copying of this file, via any medium is strictly prohibited.
         * Proprietary and confidential.
         * Written by Stone Shaw.
         */

import com.alibaba.fastjson.JSON;
import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.LogConstants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.web.dto.ComplaintFollowUser;
import com.ugoodtech.mdcc.web.dto.GroupResponse;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.AnswerDicService;
import com.ugoodtech.mdcc.web.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "question接口")
@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private StationCustomerReportRepository stationCustomerReportRepository;

    @Autowired
    private QuestionSectionRepository questionSectionRepository;

    @Autowired
    private QuestionPaperRepository questionPaperRepository;

    @Autowired
    private QuestionGroupRepository questionGroupRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private AnswerDicService answerDicService;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private AuditColorReportRepository auditColorReportRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private StationPaintReportRepository stationPaintReportRepository;

    @Autowired
    private ComplaintReportDataRepository complaintReportDataRepository;

    @Autowired
    private ComplaintSolutionDataRepository complaintSolutionDataRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    private static Logger logger = LoggerFactory.getLogger(QuestionController.class);

    private List<StationColorReport> colorReports = null;
    private List<StationCustomerReport> customerReports = null;
    private List<StationRegularServiceReport> regularServiceReports = null;
    private List<AuditColorReport> auditColorReports = null;
    private List<StationPaintReport> paintReports = null;
    private List<ComplaintReportData> complaintReportDataList = null;
    private List<ComplaintSolutionData> complaintSolutionDataList = null;

    @ApiOperation("获取报告")
    @RequestMapping(value = "/getReport_Paper", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getReport_Paper(@ApiParam("报告类型,colorReport,leanPaintReport,serviceReport,satisfactionReport")
                                        @RequestParam("paperCategory") String paperCategory) {
        QQuestionPaper qQuestionPaper = QQuestionPaper.questionPaper;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionPaper.deleted.eq(false));
        builder.and(qQuestionPaper.category.eq(QuestionPaper.QuestionPaperCategory.valueOf(paperCategory)));
        List<QuestionPaper> questionPaper = (List<QuestionPaper>) questionPaperRepository.findAll(builder);
        return JsonResponse.successResponseWithData(questionPaper);
    }


    @ApiOperation("获取报告中的section")
    @RequestMapping(value = "/getReport_Section", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getReport_Section(@ApiParam("报告类型,colorReport,leanPaintReport,serviceReport,satisfactionReport")
                                          @RequestParam("paperCategory") String paperCategory) {
        logger.info("报告类型:" + paperCategory);
        QQuestionSection qQuestionSection = QQuestionSection.questionSection;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionSection.deleted.eq(false));
        builder.and(qQuestionSection.enabled.eq(true));
        builder.and(qQuestionSection.paper.category.eq(QuestionPaper.QuestionPaperCategory.valueOf(paperCategory)));
        List<QuestionSection> sectionList = (List<QuestionSection>) questionSectionRepository.findAll(builder);
        //TODO 查询题目数量效率太低
        for (QuestionSection section : sectionList) {
            List<QuestionGroup> groupId = questionGroupRepository.getBySectionId(section.getId());
            Long questionNum = getQuestionNum(groupId);
            section.setQuestionNum(questionNum);
        }
        return JsonResponse.successResponseWithData(sectionList);
    }

    /**
     * 获取题目数量
     */
    private Long getQuestionNum(List<QuestionGroup> groupId) {
        Long questionNum = 0L;
        for (int i = 0; i < groupId.size(); i++) {
            questionNum += Long.valueOf(questionRepository.countQuestionNum(groupId.get(i).getId()).toString());
        }
        return questionNum;
    }

    @ApiOperation("获取报告中的Group")
    @RequestMapping(value = "/getReport_Group", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getReport_Group(@ApiParam("SectionId") @RequestParam("sectionId") Long sectionId) {
        logger.info("SectionId:" + sectionId);
        QQuestionGroup qQuestionGroup = QQuestionGroup.questionGroup;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionGroup.deleted.eq(false));
        builder.and(qQuestionGroup.enabled.eq(true));
        builder.and(qQuestionGroup.questionSection.id.eq(sectionId));
        List<QuestionGroup> groupList = (List<QuestionGroup>) questionGroupRepository.findAll(builder);
        return JsonResponse.successResponseWithData(groupList);
    }

    @ApiOperation("获取报告中的Question")
    @RequestMapping(value = "/getReport_Question", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getReport_Question(@ApiParam("GroupId") @RequestParam("groupId") Long groupId) {
        logger.info("GroupId:" + groupId);
        QQuestion qQuestion = QQuestion.question;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestion.deleted.eq(false));
        builder.and(qQuestion.enabled.eq(true));
        builder.and(qQuestion.qGroup.id.eq(groupId));
        List<Question> questionList = (List<Question>) questionRepository.findAll(builder);
        return JsonResponse.successResponseWithData(questionList);
    }

    @ApiOperation("获取报告中的Answer")
    @RequestMapping(value = "/getReport_Answer", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getReport_Answer(@ApiParam("QuestionId") @RequestParam("questionId") Long questionId) {
        logger.info("QuestionId:" + questionId);
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(questionId));
        List<QuestionAnswerOption> answerList = (List<QuestionAnswerOption>) questionAnswerOptionRepository.findAll(builder);
        return JsonResponse.successResponseWithData(answerList);
    }

    @ApiOperation("编辑填空题")
    @RequestMapping(value = "/editReportQuestion_GapFilling", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReportQuestion_GapFilling(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                      @ApiParam("是否必填") @RequestParam("required") boolean required,
                                                      @ApiParam("标题") @RequestParam("subject") String subject,
                                                      @ApiParam("备注信息") @RequestParam(value = "description", required = false) String description) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        if (!question.getCategory().equals(QuestionCategory.TEXT)) {
            return JsonResponse.errorResponseWithError("抱歉", "此题目不是填空题");
        }
        boolean originRequired=question.getRequired();
        String originSubject=question.getSubject();
        String originDescription=question.getDescription();

        question.setRequired(required);
        question.setSubject(subject);
        if (StringUtils.isNotEmpty(description)) {
            question.setDescription(description);
        }
        questionRepository.save(question);
        Map<String,Object> data=new HashMap<>();
        String desc="原题:";
        desc+=originSubject;
        if(!originSubject.equals(subject)){
            desc+="=>"+question.getSubject()+",";
        }
        if(originRequired!=required){
            String originRequiredStr="否";
            String requiredStr="否";
            if(originRequired){
                originRequiredStr="是";
            }
            if(required){
                requiredStr="是";
            }
            desc+="是否必填:"+originRequiredStr+"=>"+requiredStr+",";
        }

        if(!originDescription.equals(description)){
            desc+="备注信息:"+originDescription+"=>"+description+",";
        }


        data.put("str",desc);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, data);
        return JsonResponse.successResponse();
    }

    @ApiOperation("编辑选择题")
    @RequestMapping(value = "/editReportQuestion_ChoiceQuestion", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReportQuestion_ChoiceQuestion(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                          @ApiParam("是否必填") @RequestParam("required") boolean required,
                                                          @ApiParam("单选：RADIO，多选：CHECKBOX") @RequestParam("category") String category,
                                                          @ApiParam("标题") @RequestParam("subject") String subject,
                                                          @ApiParam("标准答案") @RequestParam(value = "standardAnswerIds", required = false) String standardAnswerIds,
                                                          @ApiParam("备注信息") @RequestParam(value = "description", required = false) String description) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        boolean originRequired=question.getRequired();
        String originSubject=question.getSubject();
        String originDescription=question.getDescription();


        question.setRequired(required);
        question.setCategory(QuestionCategory.valueOf(category));
        question.setSubject(subject);
        if (StringUtils.isNotEmpty(standardAnswerIds)) {
            String[] standardIds = standardAnswerIds.split(",");
            if (category.equals(QuestionCategory.RADIO)) {
                if (standardIds.length == 1) {
                    answerDicService.addRadio(Long.valueOf(standardIds[0]), questionId);
                }
            }
            if (category.equals(QuestionCategory.CHECKBOX)) {
                answerDicService.addCheckBox(standardIds, questionId);
            }
        }
        if (StringUtils.isNotEmpty(description)) {
            question.setDescription(description);
        }
        questionRepository.save(question);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);

        return JsonResponse.successResponse();
    }

    @ApiOperation("编辑选择题选项")
    @RequestMapping(value = "/editReportQuestion_Options", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReportQuestion_Options(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                   @ApiParam("答案Id") @RequestParam("answerId") Long answerId,
                                                   @ApiParam("名称") @RequestParam("name") String name,
                                                   @ApiParam("备注信息") @RequestParam(value = "description", required = false) String description) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(answerId);
        if (answerOption == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此选项");
        }
        if (!answerOption.getQuestion().getId().equals(questionId)) {
            return JsonResponse.errorResponseWithError("抱歉", "此选项不是此题目的或此题目没有此选项");
        }
        answerOption.setName(name);
        if (StringUtils.isNotEmpty(description)) {
            answerOption.setDescription(description);
            answerOption.setExtra(description);
        }
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);

        questionAnswerOptionRepository.save(answerOption);
        return JsonResponse.successResponse();
    }

    @ApiOperation("编辑上传入口")
    @RequestMapping(value = "/editReportQuestion_upload", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReportQuestion_upload(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                  @ApiParam("名称") @RequestParam("name") String name,
                                                  @ApiParam("是否必填") @RequestParam("required") boolean required,
                                                  @ApiParam("备注信息") @RequestParam(value = "description", required = false) String description) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        if (!question.getCategory().equals(QuestionCategory.UPLOAD_PHOTO)) {
            return JsonResponse.errorResponseWithError("抱歉", "此题不是需要上传照片的题目");
        }
        question.setRequired(required);
        question.setSubject(name);
        question.setDescription(description);
        questionRepository.save(question);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);

        return JsonResponse.successResponse();
    }

    @ApiOperation("编辑问答题")
    @RequestMapping(value = "/editReportQuestion_TextArea", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReportQuestion_TextArea(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                    @ApiParam("名称") @RequestParam("name") String name,
                                                    @ApiParam("是否必填") @RequestParam("required") boolean required,
                                                    @ApiParam("备注信息") @RequestParam(value = "description", required = false) String description) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        if (!question.getCategory().equals(QuestionCategory.TEXT_AREA)) {
            return JsonResponse.errorResponseWithError("抱歉", "此题不是需要上传照片的题目");
        }
        question.setRequired(required);
        question.setSubject(name);
        question.setDescription(description);
        questionRepository.save(question);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);

        return JsonResponse.successResponse();
    }

    @ApiOperation("添加选择题选项")
    @RequestMapping(value = "/addReportQuestion_Options", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse addReportQuestion_Options(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                  @ApiParam("名称") @RequestParam("name") String name,
                                                  @ApiParam("是否允许填空") @RequestParam("addText") boolean addText,
                                                  @ApiParam("结果文字") @RequestParam(value = "results") String results,
                                                  @ApiParam("建议") @RequestParam(value = "advice") String advice) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        logger.info("单选题类型：" + QuestionCategory.RADIO);
        logger.info("多选题类型：" + QuestionCategory.CHECKBOX);
        logger.info("当前题目类型：" + question.getCategory());
        QuestionAnswerOption answerOption = new QuestionAnswerOption();
        answerOption.setAddText(addText);
        answerOption.setName(name);
        answerOption.setQuestion(question);
        answerOption.setAdvice(advice);
        answerOption.setEvaluation(results);
        questionAnswerOptionRepository.save(answerOption);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_add, LogConstants.business_type_question, null, null);

        return JsonResponse.successResponse();
    }

    @ApiOperation("删除选择题选项")
    @RequestMapping(value = "/delReportQuestion_Options", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse delReportQuestion_Options(@ApiParam("问题ID") @RequestParam("questionId") Long questionId,
                                                  @ApiParam("答案ID") @RequestParam("answerId") Long answerId) {
        Question question = questionRepository.findOne(questionId);
        if (question == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此题目");
        }
        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(answerId);
        if (answerOption == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此选项");
        }
        if (!answerOption.getQuestion().getId().equals(question.getId())) {
            return JsonResponse.errorResponseWithError("抱歉", "此选项不是这道选择题");
        }
        answerOption.setEnabled(false);
        answerOption.setDeleted(true);
        questionAnswerOptionRepository.save(answerOption);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_delete, LogConstants.business_type_question, null, null);

        return JsonResponse.successResponse();
    }

    @ApiOperation("编辑Section大题题目名称")
    @RequestMapping(value = "/editReport_SectionName", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReport_SectionName(Long sectionId, String sectionName) {
        QQuestionSection qQuestionSection = QQuestionSection.questionSection;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionSection.deleted.eq(false));
        builder.and(qQuestionSection.enabled.eq(true));
        builder.and(qQuestionSection.id.eq(sectionId));
        QuestionSection questionSection = questionSectionRepository.findOne(builder);
        if (qQuestionSection == null) {
            return JsonResponse.errorResponseWithError("抱歉", "为找到此章节");
        }
        questionSection.setName(sectionName);
        questionSectionRepository.save(questionSection);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);
        return JsonResponse.successResponse();
    }

    @ApiOperation("编辑Group大题题目名称")
    @RequestMapping(value = "/editReport_GroupName", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse editReport_GroupName(Long groupId, String groupName) {
        QQuestionGroup qQuestionGroup = QQuestionGroup.questionGroup;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionGroup.deleted.eq(false));
        builder.and(qQuestionGroup.enabled.eq(true));
        builder.and(qQuestionGroup.id.eq(groupId));
        QuestionGroup questionGroup = questionGroupRepository.findOne(builder);
        if (questionGroup == null) {
            return JsonResponse.errorResponseWithError("抱歉", "为找到此大题");
        }
        questionGroup.setTitle(groupName);
        questionGroupRepository.save(questionGroup);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);
        return JsonResponse.successResponse();
    }

    @ApiOperation("修改创建原因")
    @RequestMapping(value = "/changeReson", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse changeReson(Long paperId, String reson) {
        QQuestionPaper qQuestionPaper = QQuestionPaper.questionPaper;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionPaper.deleted.eq(false));
        builder.and(qQuestionPaper.enabled.eq(true));
        builder.and(qQuestionPaper.id.eq(paperId));
        QuestionPaper paper = questionPaperRepository.findOne(builder);
        if (paper == null) {
            return JsonResponse.errorResponseWithError("抱歉", "为找到此报告");
        }
        paper.setReason(reson);
        questionPaperRepository.save(paper);
        userLogService.addUserLog(this.getCurrentUser(), null, LogConstants.operate_type_edit, LogConstants.business_type_question, null, null);
        return JsonResponse.successResponse();
    }


    @ApiOperation("获取章节分组和题目")
    @RequestMapping(value = "/getGroups", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getGroups(
            @ApiParam("章节id") @RequestParam(required = false) Long sectionId,
            @ApiParam("报告id") @RequestParam(required = false) Long reportId,
            @ApiParam("报告类型:colorReport,leanPaintReport,serviceReport,saleServiceReport,satisfactionReport,complaint_report,complaint_solution_report") @RequestParam(required = false) String reportType,
            @ApiParam("complete:已经通过审核的报告,unfinshed:待审核或者审核不通过") @RequestParam(required = false) String completeUnfinshed
    ) {
        logger.info("开始调用此方法");
        Long time = System.currentTimeMillis();
        if (reportId != null && reportId != 0) {
            if (reportType.equals(Constants.TECHCOLOR_REPORT)) {
                if (completeUnfinshed.equals("complete")) {
                    colorReports = stationColorReportRepository.selectAnswer(reportId);
                } else if (completeUnfinshed.equals("unfinshed")) {
                    auditColorReports = auditColorReportRepository.selectAnswer(reportId);
                }
            }
            if (reportType.equals(Constants.CUSTOMER_REPORT)) {
                customerReports = stationCustomerReportRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.SERVICE_REPORT)) {
                regularServiceReports = stationRegularServiceReportRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.PAINT_REPORT)) {
                paintReports = stationPaintReportRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.COMPLAINT_REPORT)) {
                complaintReportDataList = complaintReportDataRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.COMPLAINT_SOLUTION_REPORT)) {
                complaintSolutionDataList = complaintSolutionDataRepository.selectAnswer(reportId);
            }
        }
        List<QuestionGroup> questionGroups = questionService.getQuestions(sectionId);
        List<Object> dataList = new ArrayList<>();
        for (QuestionGroup questionGroup : questionGroups) {
            dataList.add(GroupResponse.turnGroupToResponse(questionGroup));
            List<Question> questionGroupList = questionService.getQuestionsByGroup(questionGroup.getId());
            for (Question question : questionGroupList) {
                Long question_id = Long.valueOf(question.getId());
                if (question_id == 146 || question_id == 147 || question_id == 148 || question_id == 149 || question_id == 150 || question_id == 151 ||
                        question_id == 152 || question_id == 153 || question_id == 154) {
                    continue;
                }
                if (reportId != null && reportId != 0) {
                    question = selectAnswer(question, reportId);
                }
                List<QuestionAnswerOption> questionAnswerOptions = questionService.getOptionsByGroup(question.getId());
                question.setOptions(questionAnswerOptions);
                List<Question> questions = new ArrayList<>();
                questions.add(question);
                if (QuestionCategory.TEXT.equals(question.getCategory())) {
                    Map<String, Object> textQuestion = new HashMap<>();
                    textQuestion.put("category", question.getCategory().name());
                    textQuestion.put("required", Boolean.valueOf(question.getRequired()));
                    textQuestion.put("type", "question");
                    textQuestion.put("inputList", questions);
                    dataList.add(textQuestion);
                } else {
                    dataList.add(question);
                }
            }
        }
        colorReports = null;
        customerReports = null;
        regularServiceReports = null;
        paintReports = null;
        complaintReportDataList = null;
        complaintSolutionDataList = null;
        logger.info("调用完毕");
        logger.info("总耗时：" + (System.currentTimeMillis() - time));
        return JsonResponse.successResponseWithData(dataList);
    }


    private Question selectAnswer(Question question, Long report_id) {
        if (complaintSolutionDataList != null) {
            for (ComplaintSolutionData report : complaintSolutionDataList) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else if (StringUtils.isNotEmpty(report.getAnswerOther2())) {
                        question.setAnswer_other2(report.getAnswerOther2());
                    } else {
                        question.setAnswer_other("");
                        question.setAnswer_other2("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerId())) {
                        if (report.getAnswerId().indexOf(",") != -1) {
                            question.setSelected(report.getAnswerId());
                        } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                            question.setSelected(report.getAnswerId());
                        } else {
                            question.setSelected("");
                        }
                        if (report.getAnswerId().equals("444")) {
                            if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                                List<ComplaintFollowUser> followUserList = new ArrayList<>();
                                for (String userId : report.getAnswerOther().split(",")) {
                                    UserProfile userProfile = userProfileRepository.findByUserId(Long.valueOf(userId));
                                    if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
                                        ComplaintFollowUser followUser = new ComplaintFollowUser(Long.valueOf(userId), userProfile.getRealName());
                                        followUserList.add(followUser);
                                    }
                                }
                                question.setAnswer_other(JSON.toJSONString(followUserList));
                            } else if (StringUtils.isNotEmpty(report.getAnswerOther2()) && report.getAnswerOther2().indexOf(",") == -1) {
                                UserProfile userProfile = userProfileRepository.findByUserId(Long.valueOf(report.getAnswerOther2()));
                                ComplaintFollowUser followUser = new ComplaintFollowUser(Long.valueOf(report.getAnswerOther2()), userProfile.getRealName());
                                question.setAnswer_other2(JSON.toJSONString(followUser));
                            }
                        }
                    }
                }
            }
            return question;
        }
        if (complaintReportDataList != null) {
            for (ComplaintReportData report : complaintReportDataList) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerId())) {
                        if (report.getAnswerId().indexOf(",") != -1) {
                            question.setSelected(report.getAnswerId());
                        } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                            question.setSelected(report.getAnswerId());
                        } else {
                            question.setSelected("");
                        }
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (report.getQuestionId() == 194L) {
                        StringBuffer stringBuffer = new StringBuffer("");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            String[] ids = report.getAnswerId().split(",");
                            for (String id : ids) {
                                if (StringUtils.isNotEmpty(id)) {
                                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                        stringBuffer.append(answerOption.getName() + ",");
                                    }
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                            question.setAnswer_other2(report.getAnswerOther());
                        }
                        question.setAnswer_other(stringBuffer.toString());
                    }
                }
            }
            return question;
        }
        if (customerReports != null) {
            for (StationCustomerReport report : customerReports) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                        QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(report.getAnswerId()));
                        String score = questionAnswerOption.getName();
                        if (score.equals("20%")) {
                            question.setAnswer_other("1");
                        }
                        if (score.equals("40%")) {
                            question.setAnswer_other("2");
                        }
                        if (score.equals("60%")) {
                            question.setAnswer_other("3");
                        }
                        if (score.equals("80%")) {
                            question.setAnswer_other("4");
                        }
                        if (score.equals("100%")) {
                            question.setAnswer_other("5");
                        }
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }
        if (paintReports != null) {
            for (StationPaintReport report : paintReports) {

                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId() != null && report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (StringUtils.isNotEmpty(report.getAnswerId()) && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }
        if (regularServiceReports != null) {
            for (StationRegularServiceReport report : regularServiceReports) {

                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (report.getQuestionId() == 155L) {
                        QStationReport qStationReport = QStationReport.stationReport;
                        BooleanBuilder builder = new BooleanBuilder();
                        builder.and(qStationReport.deleted.eq(false));
                        builder.and(qStationReport.enabled.eq(true));
                        builder.and(qStationReport.id.eq(report_id));
                        StationReport stationReport = stationReportRepository.findOne(builder);
                        if (stationReport != null && StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                            question.setAnswer_other2(stationReport.getAnswerOther());
                        }
                    }
                    if (report.getQuestionId() == 156L) {
                        QStationReport qStationReport = QStationReport.stationReport;
                        BooleanBuilder builder = new BooleanBuilder();
                        builder.and(qStationReport.deleted.eq(false));
                        builder.and(qStationReport.enabled.eq(true));
                        builder.and(qStationReport.id.eq(report_id));
                        StationReport stationReport = stationReportRepository.findOne(builder);
                        if (stationReport != null && StringUtils.isNotEmpty(stationReport.getDeparmentIds())) {
                            String[] parentUserIds = stationReport.getDeparmentIds().split(",");
                            StringBuffer stringBuffer = new StringBuffer();
                            for (String id : parentUserIds) {
                                Department department = null;
                                if (StringUtils.isNotEmpty(id)) {
                                    department = departmentRepository.findOne(Long.valueOf(id));
                                }
                                if (department != null) {
                                    stringBuffer.append((department.getTotalName() + ","));
                                }
                            }
                            question.setAnswer_other(stringBuffer.toString());
                        }
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerId()) && report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (StringUtils.isNotEmpty(report.getAnswerId()) && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }
        if (colorReports != null) {
            for (StationColorReport report : colorReports) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId() != null && report.getAnswerId().indexOf(",") != -1) {
                        String[] anwserIds = report.getAnswerId().split(",");
                        StringBuffer stringBuffer = new StringBuffer();
                        for (String id : anwserIds) {
//                            if (StringUtils.isNotEmpty(id)) {
//                                QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
//                                stringBuffer.append(questionAnswerOption.getName() + ",");
//                            } else if (StringUtils.isNotEmpty(report.getAnswerOther())) {
//                                stringBuffer.append(report.getAnswerOther() + ",");
//                            }
                            stringBuffer.append(id + ",");
                        }
                        question.setSelected(stringBuffer.toString());
//                    } else if (StringUtils.isNotEmpty(report.getAnswerId()) && Long.valueOf(report.getAnswerId()) != 0) {
//                        QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(report.getAnswerId()));
//                        if (questionAnswerOption != null) {
//                            if (StringUtils.isNotEmpty(questionAnswerOption.getName())) {
//                                question.setSelected(questionAnswerOption.getName());
//                            } else {
//                                question.setSelected("");
//                            }
//                        }
                    } else {
                        question.setSelected(report.getAnswerId() == null ? "" : report.getAnswerId());
                    }
                    if (report.getQuestionId() == 8L) {
                        StringBuffer stringBuffer = new StringBuffer("");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            String[] ids = report.getAnswerId().split(",");
                            for (String id : ids) {
                                if (StringUtils.isNotEmpty(id)) {
                                    QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                    if (questionAnswerOption!=null && questionAnswerOption.getName()!=null) {
                                        if (questionAnswerOption.getName().indexOf("其他") != -1) {
                                            if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                                                stringBuffer.append("其他品牌(" + report.getAnswerOther() + ")");
                                            }
                                        } else {
                                            stringBuffer.append(questionAnswerOption.getName() + ",");
                                        }
                                    }
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                            question.setAnswer_other2(report.getAnswerOther());
                        }
                        question.setAnswer_other(stringBuffer.toString());
                    }
                }
            }
            return question;
        }

        if (auditColorReports != null) {
            for (AuditColorReport report : auditColorReports) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId() != null && report.getAnswerId().indexOf(",") != -1) {
                        String[] anwserIds = report.getAnswerId().split(",");
                        StringBuffer stringBuffer = new StringBuffer();
                        for (String id : anwserIds) {
//                            if (StringUtils.isNotEmpty(id)) {
//                                QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
//                                if (questionAnswerOption.getName().indexOf("其他") != -1) {
//                                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
//                                        stringBuffer.append("其他品牌(" + report.getAnswerOther() + ")");
//                                    }
//                                } else {
                            stringBuffer.append(id + ",");
//                                }
//                            }

                        }
                        question.setSelected(stringBuffer.toString());
//                    } else if (StringUtils.isNotEmpty(report.getAnswerId()) && Long.valueOf(report.getAnswerId()) != 0) {
//                        QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(report.getAnswerId()));
//                        if (questionAnswerOption != null) {
//                            if (StringUtils.isNotEmpty(questionAnswerOption.getName())) {
//                                question.setSelected(questionAnswerOption.getName());
//                            } else {
//                                question.setSelected("");
//                            }
//                        }
                    } else {
                        question.setSelected(report.getAnswerId() == null ? "" : report.getAnswerId());
                    }
                    if (report.getQuestionId() == 8L) {
                        StringBuffer stringBuffer = new StringBuffer("");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            String[] ids = report.getAnswerId().split(",");
                            for (String id : ids) {
                                if (StringUtils.isNotEmpty(id)) {
                                    QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                    if (questionAnswerOption!=null && questionAnswerOption.getName()!=null) {
                                        if (questionAnswerOption.getName().indexOf("其他") != -1) {
                                            if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                                                stringBuffer.append("其他品牌(" + report.getAnswerOther() + ")");
                                            }
                                        } else {
                                            stringBuffer.append(questionAnswerOption.getName() + ",");
                                        }
                                    }
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                            question.setAnswer_other2(report.getAnswerOther());
                        }
                        question.setAnswer_other(stringBuffer.toString());
                    }
                }
            }
            return question;
        }

        return question;
    }


}
